﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ikvm.extensions;
using java.lang;
using java.util.regex;
using Double = System.Double;
using Math = System.Math;
using Object = System.Object;
using String = System.String;

namespace Game.utils
{
    public class Strings
    {
        /**
         * Method stripSlashes.
         * @param s String
         * @return String
         */
        public static String stripSlashes(String s)
        {
            if (s == null)
            {
                return "";
            }
            s = s.replace("\\'", "'");
            s = s.replace("\\\\", "\\");
            return s;
        }

        /**
         * Method parseBoolean.
         * @param x Object
         * @return Boolean
         */
        public static bool parseBoolean(Object x)
	{
		if (x == null)
		{
			return false;
		}
            var number = x as Number;
            if (number != null)
		{
			return number.intValue() > 0;
		}
        if (x is bool)
		{
            return (bool) x;
		}
		if (x is Double)
		{
			return Math.Abs((Double) x) < 0.00001;
		}
		return !java.lang.String.valueOf(x).isEmpty();
	}

        /**
         * Field tr.
         */
        private static String[] tr;
        /**
         * Field trb.
         */
        private static String[] trb;
        /**
         * Field trcode.
         */
        private static String[] trcode;

        /**
         * Method reload.
         */
        public static void reload()
        {
            // TODO: DROP STRINGS.JAVA!
            /*
             * try { String[] pairs = FileUtils.readFileToString(new File(Config.DATAPACK_ROOT, "data/translit.txt")).split("\n"); tr = new String[pairs.length * 2]; for (int i = 0; i < pairs.length; i++) { String[] ss = pairs[i].split(" +"); tr[i * 2] = ss[0]; tr[(i * 2) + 1] = ss[1]; } pairs =
             * FileUtils.readFileToString(new File(Config.DATAPACK_ROOT, "data/translit_back.txt")).split("\n"); trb = new String[pairs.length * 2]; for (int i = 0; i < pairs.length; i++) { String[] ss = pairs[i].split(" +"); trb[i * 2] = ss[0]; trb[(i * 2) + 1] = ss[1]; } pairs =
             * FileUtils.readFileToString(new File(Config.DATAPACK_ROOT, "data/transcode.txt")).split("\n"); trcode = new String[pairs.length * 2]; for (int i = 0; i < pairs.length; i++) { String[] ss = pairs[i].split(" +"); trcode[i * 2] = ss[0]; trcode[(i * 2) + 1] = ss[1]; } } catch (IOException e) {
             * _log.error("", e); } _log.info("Loaded " + (tr.length + tr.length + trcode.length) + " translit entries.");
             */
        }

        /**
         * Method translit.
         * @param s String
         * @return String
         */
        public static String translit(String s)
        {
            for (int i = 0; i < tr.Length; i += 2)
            {
                s = s.replace(tr[i], tr[i + 1]);
            }
            return s;
        }

        /**
         * Method fromTranslit.
         * @param s String
         * @param type int
         * @return String
         */
        public static String fromTranslit(String s, int type)
        {
            if (type == 1)
            {
                for (int i = 0; i < trb.Length; i += 2)
                {
                    s = s.replace(trb[i], trb[i + 1]);
                }
            }
            else if (type == 2)
            {
                for (int i = 0; i < trcode.Length; i += 2)
                {
                    s = s.replace(trcode[i], trcode[i + 1]);
                }
            }
            return s;
        }

        /**
         * Method replace.
         * @param str String
         * @param regex String
         * @param flags int
         * @param replace String
         * @return String
         */
        public static String replace(String str, String regex, int flags, String replace)
        {
            return Pattern.compile(regex, flags).matcher(str).replaceAll(replace);
        }

        /**
         * Method matches.
         * @param str String
         * @param regex String
         * @param flags int
         * @return boolean
         */
        public static bool matches(String str, String regex, int flags)
        {
            return Pattern.compile(regex, flags).matcher(str).matches();
        }

        /**
         * Method bbParse.
         * @param s String
         * @return String
         */
        public static String bbParse(String s)
        {
            if (s == null)
            {
                return null;
            }
            s = s.replace("\r", "");
            s = s.replaceAll("(\\s|\"|\'|\\(|^|\n)\\*(.*?)\\*(\\s|\"|\'|\\)|\\?|\\.|!|:|;|,|$|\n)", "$1<font color=\"LEVEL\">$2</font>$3");
            s = s.replaceAll("(\\s|\"|\'|\\(|^|\n)\\$(.*?)\\$(\\s|\"|\'|\\)|\\?|\\.|!|:|;|,|$|\n)", "$1<font color=\"00FFFF\">$2</font>$3");
            s = replace(s, "^!(.*?)$", Pattern.MULTILINE, "<font color=\"FFFFFF\">$1</font>\n\n");
            s = s.replaceAll("%%\\s*\n", "<br1>");
            s = s.replaceAll("\n\n+", "<br>");
            s = replace(s, "\\[([^\\]\\|]*?)\\|([^\\]]*?)\\]", Pattern.DOTALL, "<a action=\"bypass -h $1\">$2</a>");
            s = s.replaceAll(" @", "\" msg=\"");
            return s;
        }

        /**
         * Method joinStrings.
         * @param glueStr String
         * @param strings String[]
         * @param startIdx int
         * @param maxCount int
         * @return String
         */
        public static String joinStrings(String glueStr, String[] strings, int startIdx, int maxCount)
        {
            var result = "";
            if (startIdx < 0)
            {
                startIdx += strings.Length;
                if (startIdx < 0)
                {
                    return result;
                }
            }
            while ((startIdx < strings.Length) && (maxCount != 0))
            {
                if (!result.isEmpty() && (glueStr != null) && !glueStr.isEmpty())
                {
                    result += glueStr;
                }
                result += strings[startIdx++];
                maxCount--;
            }
            return result;
        }

        /**
         * Method joinStrings.
         * @param glueStr String
         * @param strings String[]
         * @param startIdx int
         * @return String
         */
        public static String joinStrings(String glueStr, String[] strings, int startIdx)
        {
            return joinStrings(glueStr, strings, startIdx, -1);
        }

        /**
         * Method joinStrings.
         * @param glueStr String
         * @param strings String[]
         * @return String
         */
        public static String joinStrings(String glueStr, String[] strings)
        {
            return joinStrings(glueStr, strings, 0);
        }

        /**
         * Method stripToSingleLine.
         * @param s String
         * @return String
         */
        public static String stripToSingleLine(String s)
        {
            if (s.isEmpty())
            {
                return s;
            }
            s = s.replaceAll("\\\\n", "\n");
            int i = s.indexOf("\n");
            if (i > -1)
            {
                s = s.substring(0, i);
            }
            return s;
        }
    }

}
